﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace Bot.Dynamics365.Extension
{
    public static class HttpRequestMessageExtensions
    {
        public static HttpRequestMessage AddContent(this HttpRequestMessage request, string content)
        {
            request.Content = new StringContent(content, Encoding.UTF8, "application/json");
            return request;
        }

        public static HttpRequestMessage AddAuthorization(this HttpRequestMessage request, string token)
        {
            request.Headers.Add("Authorization", $"Bearer {token}");
            return request;
        }

        public static HttpRequestMessage AddForm(this HttpRequestMessage request, IEnumerable<KeyValuePair<string, string>> nameValueCollection)
        {
            request.Content = new FormUrlEncodedContent(nameValueCollection);
            return request;
        }

        public static HttpRequestMessage AddHeaders(this HttpRequestMessage request, Dictionary<string, string> headers)
        {
            if (headers != null)
            {
                foreach(var header in headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }

            return request;
        }
    }
}
